package com.echo.fm.platform.auth;

import com.echo.fm.content.systemAction.model.SystemAction;
import com.echo.fm.platform.auth.model.Authority;
import com.echo.fm.platform.auth.model.LoginPass;
import com.echo.fm.platform.auth.model.RoleAuth;
import com.echo.fm.platform.auth.model.SystemUser;
import com.echo.fm.user.role.model.Role;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

@Repository("authPersistence")
public class DAOAuthPersistenceImpl implements IAuthPersistence {
	@Autowired
	@Qualifier("sessionFactory")
	protected SessionFactory sessionFactory;

	public SystemUser searchSystemUserByUsername(String userName) throws ErrorException {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			SystemUser systemUser = (SystemUser) session.createQuery("from SystemUser where username=:username")
					.setString("username", userName).uniqueResult();
			return systemUser;
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public boolean checkPassword(String uid, String password) throws ErrorException {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			String query = "select count(1) from system_user where id = '%s' and password = '%s'";
			query = String.format(query, uid, password);
			BigInteger count = (BigInteger) session.createSQLQuery(query).uniqueResult();
			if (count.intValue() == 1) {
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void registSystemUser(SystemUser systemUser) throws ErrorException {
		Session session = null;
		Transaction tx = null;
		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			session.save(systemUser);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public Authority searchAuthorityByToken(String token) throws ErrorException {
		return null;
	}

	public Authority searchAuthorityByUId(String uid) throws ErrorException {
		return null;
	}

	public List<Authority> searchAuthorityList() throws ErrorException {
		return null;
	}

	public List<Authority> searchAuthorityList(boolean isTest) throws ErrorException {
		return null;
	}

	public void updateAuthority(Authority authority) throws ErrorException {

	}

	public String searchUserIdByUsername(String username) throws ErrorException {
		return null;
	}

	public LoginPass searchLoginPassByUId(String userId) throws ErrorException {
		return null;
	}

	public List<RoleAuth> listRoleAuth(Integer roleId) throws ErrorException {
		return null;
	}

	public List<Role> searchRoleList(List<Criterion> criterions) throws ErrorException {
		return null;
	}

	public List<Role> searchRoleList(SystemUser user) throws ErrorException {
		return null;
	}

	public boolean checkLogin(String id, String password) throws ErrorException {
		return false;
	}

	public List<SystemAction> getAllSystemActions() throws ErrorException {
		return null;
	}

	public boolean checkAppuser(String uid, String password) throws ErrorException {
		return false;
	}
}
